<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="20">
            <el-form-item label="业态名称" prop="name"
                :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
			        <el-input v-model="inputForm.name" placeholder="请填写名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="23" v-for="language in languages" :key="language.id">
            <el-form-item :label="'业态名称'+language.label" :prop="'name'+language.value"
                :rules="[
                 ]">
              <el-input v-model="inputForm['name'+language.value]" :placeholder="'请填写业态名称'+language.label"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="20">
            <el-form-item label="地图模型颜色" prop="mapColor"
                :rules="[
                 ]">
              <el-color-picker v-model="inputForm.mapColor"></el-color-picker>
           </el-form-item>
        </el-col>
        <el-col :span="20">
            <el-form-item label="排序" prop="sort"
                :rules="[
                  {required: true, message:'排序不能为空', trigger:'blur'},
                  {validator: validator.isNumber, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col>
       <el-col :span="20" v-for="(item, index) in files" :key='index'>
            <el-form-item :label="item.label" :prop="item.value"
                 :rules="[
                  ]">
               <el-upload-ext
                 :limit = "1"
                 :accept = "item.format"
                 :fileType = "item.fileType"
                 :width = "item.width"
                 :height = "item.height"
                 v-model = "inputForm.file[item.value]"
               >
               </el-upload-ext>
            </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import ElUploadExt from '@/components/elUploadExt'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          parent: {
            id: ''
          },
          name: '',
          name1: '',
          name2: '',
          name3: '',
          sort: '',
          file: {
          },
          filesAll: '',
          mapColor: '',
          areaType: ''
        }
      }
    },
    components: {
      SelectTree,
      ElUploadExt
    },
    computed: {
      languages: {
        get () {
          return this.$store.state.user.languages
        }
      },
      files: {
        get () {
          return this.$store.state.config.merchantsType
        }
      }
    },
    methods: {
      init (method, obj) {
        this.method = method
        this.inputForm.id = obj.id
        if (method === 'add') {
          this.title = '新建业态'
        } else if (method === 'addChild') {
          this.title = '添加下级业态'
        } else if (method === 'edit') {
          this.title = '修改业态'
        } else if (method === 'view') {
          this.title = '查看业态'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (this.files) {
            this.files.forEach((item) => {
              this.inputForm.file[item.keyStr] = ''
            })
          }
          this.inputForm.parent.id = obj.parent.id
          this.inputForm.parent.name = obj.parent.name
          this.inputForm.file = {}
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/merchantstype/jfMerchantsType/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfMerchantsType)
              if(data.jfMerchantsType.filesAll && data.jfMerchantsType.filesAll.length > 0) {
                this.inputForm.file = JSON.parse(data.jfMerchantsType.filesAll)
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.filesAll = JSON.stringify(this.inputForm.file)
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/merchantstype/jfMerchantsType/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
